package com.xiangxiangjd.utils.erweima;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtil {

    /**
     *   画布的大小
     */
    private static int width=600;
    private static int height=600;
    /**
     *   二维码大小
     */
    private static int qwWidth=570;
    private static int qwHeight=570;


    /**
     * 二维码在画布的位子
     */

    private static int qwX=width-qwWidth;  //左右
    private static int qwY=-30;  //上下



    /**
     * 字体设置
     */

    private static int fontSize=25;//字体大小
    private static int fontSpace=30; //字体之间的距离
    private static int fontSpaceQW=65; //字体和二维码的距离





    /**
     * @param str     生产的图片文字
     * @param oldPath 原图片保存路径
     * @param newPath 新图片保存路径
     * @return
     * @throws IOException
     */
    public static void create(String str, String oldPath, String newPath) {


        try {
            File oldFile = new File(oldPath);
            Image image = ImageIO.read(oldFile);

            File file = new File(newPath);
            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bi.createGraphics();
            g2.setBackground(Color.WHITE);
            //画布大小
            g2.clearRect(0, 0, width, height);
            //二维位子调整
            g2.drawImage(image, qwX, qwY, qwWidth, qwHeight, null); //这里减去25是为了防止字和图重合


            /** 设置生成图片的文字样式 * */
         //   Font font = new Font("微软雅黑", Font.BOLD, 22);
            Font font = new Font("宋体", Font.BOLD, fontSize);
            g2.setFont(font);

            g2.setPaint(Color.BLACK);
            /** 设置字体在图片中的位置 在这里是居中* */
            FontRenderContext context = g2.getFontRenderContext();


            System.out.println("开始读取文本********************");
            System.out.println(str);

            //按行 拆分文本
            String[] split = str.split("\\r?\\n");

            System.out.println("一共：" + split.length + "行");


            //循环遍历  长度除以15 计算行数
            for (int i = 0; i < split.length; i++) {

                System.out.println("第"+i+"行："+split[i]);

                //获取第一行
                String hang = split[i];

                Rectangle2D bounds = font.getStringBounds(hang, context);
                double x = (width - bounds.getWidth()) / 2 + 10;
                //double y = (height - bounds.getHeight()) / 2; //Y轴居中
                double y = (height - bounds.getHeight());
                double ascent = -bounds.getY();
                double baseY = y + ascent;


                /**
                 * 第一行字体
                 */

                /** 防止生成的文字带有锯齿 * */
                g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);


                System.out.println(hang);
                /** 在图片上生成文字 * */
                g2.drawString(hang, (int) x, (int) baseY - fontSpaceQW + (i * fontSpace));


            }

            ImageIO.write(bi, "jpg", file);
            System.out.println("图片生成成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {


        try {

            String str = "间的哈可接受的\n" +
                    "埃里克斯搭建聊卡萨丁奥术大师\n" +
                    "马拉松看得见啊";

            ImageUtil.create(str, "C:\\Users\\001\\Desktop\\download.jpg ", "C:\\Users\\001\\Desktop\\111.png");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}
